<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use App\Facades\UniqID;
use App\Models\MeetDay;
use App\Models\MeetHour;
use Illuminate\Console\Command;
use App\Modules\Admin\Models\AdminUser;

class MeetDayCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'day:generator {venue_id : 场景id} {days? : 生成天数(选填,默认30)}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '预约日期生成脚本';

    private $venueId = 0;
    private $dayNum = 0;
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->info('Running Day Generator ...');

        $this->venueId = trim($this->argument('venue_id'));
        $this->dayNum = trim($this->argument('days'));

        if (empty($this->dayNum)) {
            $this->dayNum = 30;
        }

        // 查询现在日期
        $meetDay = $this->getLastDay($this->venueId);
        if ($meetDay) {
            $lastDay = $meetDay->day;
            $adminUid = $this->getAdminUid('admin');

            for ($i = 1; $i <= $this->dayNum; $i++) {
                $tempData = [];
                $day = Carbon::parse($lastDay)->addDays($i)->toDateTimeString();
                $tempData['venue_id'] = $this->venueId;
                $tempData['day'] = $day;
                $tempData['publisher_uid'] = $adminUid;
                // 如果 venue_id = 1, 则需要周一闭馆
                $tempData['status'] = 1;
                if (1 == $this->venueId) {
                    $dt = Carbon::parse($day)->dayOfWeek;
                    if ($dt == 1) {
                        $tempData['status'] = 0;
                    }
                }

                $hours = $this->getHour($this->venueId);
                \DB::transaction(function () use ($tempData, $hours, $adminUid) {

                    $meetDay = MeetDay::create($tempData);

                    $hourArr = [];
                    foreach ($hours as $item) {
                        $item['meet_hour_id'] = UniqID::nextId();
                        $item['venue_id'] = $this->venueId;
                        $item['meet_day_id'] = $meetDay->id;
                        $item['publisher_uid'] = $adminUid;
                        $item['created_at'] = date('Y-m-d H:i:s', time());
                        $item['updated_at'] = date('Y-m-d H:i:s', time());
                        array_push($hourArr, $item);
                    }
                    MeetHour::insert($hourArr);
                   

                    return true;
                });
                $this->info('场景id:' .$this->venueId . ' 新增 ' . $day. '预约日期数据');

            }
        }
        $this->info('Done Day Generator');

        return true;
    }

    public function getHour($venueId)
    {
        $result = [];
        $temp = [];
        $tempTwo = [];
        $tempThree = [];

        $temp['start'] = '9:00';
        $temp['end'] = '12:00';
        array_push($result, $temp);

        // if (1 == $venueId) {
        //     $tempTwo['start'] = '14:00';
        // } else {
            $tempTwo['start'] = '12:00';
        // }
        $tempTwo['end'] = '16:00';
        array_push($result, $tempTwo);

        $tempThree['start'] = '16:00';
        $tempThree['end'] = '19:00';
        array_push($result, $tempThree);
        return $result;
    }

    public function getAdminUid($account)
    {
        if (empty($account)) {
            return null;
        }
        $adminUser = AdminUser::where(['account' => $account])->first();
        return $adminUser->admin_uid;

    }

    public function getLastDay($venueId)
    {
        if (empty($venueId)) {
            return null;
        }

        $meetDay = MeetDay::where(['venue_id' => $venueId])->orderBy('day', 'desc')->first();
        return $meetDay;
    }
}
